<?php

class Purchase_m extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    //purchase
    public function get_purchase($page=0,$id=null){
        $offset = 20;
        $start = $page*$offset;
        $this->db->limit($offset,$start);
        $this->db->order_by('modified_date','desc');
        if(!empty($id)){
            $this->db->where('id',$id);
        }
        return $this->db->get('purchase');
    }
    
    public function get_purchase_count(){
        return $this->db->count_all_results('purchase');
    }
    
    public function get_purchase_item($id){
        $this->db->select('purchase_item.*');
        $this->db->select('products.price');
        $this->db->select('products.name');
        $this->db->join('products','purchase_item.product_id=products.id','left outer');
        $this->db->where('purchase_id',$id);
        return $this->db->get('purchase_item');
    }
    
    public function delete($id){
        $this->delete_item($id);
        $this->db->where('id',$id,FALSE);
        $this->db->delete('purchase');
        
    }
    
    public function delete_item($purchase_id){
        $this->db->where('purchase_id',$purchase_id,FALSE);
        $this->db->delete('purchase_item');
    }
}